#ifndef _USER_MANAGE_CPP
#define _USER_MANAGE_CPP

#include "UserManage.h"
#include <string.h>
#include <list>
#include "MeetingManage.h"
using std::string;
using std::list;

// ===== Static object initialization =====
bool UserManage::instanceFlag_ = false;
UserManage* UserManage::instance_ = NULL;


UserManage::UserManage() {
	users_.clear();
}

UserManage* UserManage::getInstance() {

	// check if the instance has been initialized ..
	if(instanceFlag_==false) {
		instance_ = new UserManage();
		instanceFlag_ = true;
	}

	// return the instance_ ..
	return instance_;
}

UserManage::~UserManage() {
	delete instance_;
	instanceFlag_ = false;
}

User* UserManage::findUserByName( string name ) {
	// Find a user by its username, return a user pointer

	list<User>::iterator it;

	// loop find the user by the username ..
	for(it=users_.begin();it!=users_.end();++it) {
		if((*it).getName()==name) return &(*it);
	}

	// not found, return NULL ..
	return NULL;
}

bool UserManage::createUser( string name, string password, string email, string phone ) {
	// Create a new user, and add to attach the user to its management ..

	// check if name conflict ..
	if(this->findUserByName(name)!=NULL) return false; // conflict, return false

	// create a new user, and initialize it ..
	User usr(name, password, email, phone);

	// attach ..
	users_.push_back(usr);

	// successful, return true ..
	return true;
}

bool UserManage::deleteUser( User u ) {
	// delete a user by its identity ..

	list<User>::iterator it;


	// find the user ..
	for(it=users_.begin();it!=users_.end();++it) {
		if((*it).getName()==u.getName()) {
			// user found, check if any meeting remains ..
			if((MeetingManage::getInstance()->listAllMeetings(it->getName())).size()!=0) {
				return false;
			}

			// no meetings, remove it from the management of it
			users_.erase(it);

			// successful, return true ..
			return true;
		}
	}

	// not found, return false ..
	return false;
}

bool UserManage::updateUserPassword( User u, string newPassword ) {
	// Update the password of a user

	list<User>::iterator it;


	// find the user ..
	for(it=users_.begin();it!=users_.end();++it) {
		if((*it).getName()==u.getName()) break;
	}

	// succeed, return true ..
	if(it!=users_.end()) {
		(*it).setPassword(newPassword);
		return true;
	}

	// fail, return false ..
	return false;
}

bool UserManage::updateUserEmail( User u, string newEmail ) {
	// Update the email of a user 

	list<User>::iterator it;


	// find the user ..
	for(it=users_.begin();it!=users_.end();++it) {
		if((*it).getName()==u.getName()) break;
	}

	// succeed, return true ..
	if(it!=users_.end()) {
		(*it).setEmail(newEmail); 
		return true;
	}

	// fail, return false ..
	return false;
}

bool UserManage::updateUserPhone( User u, string newPhone ) {
	// Update the phone number of a user 

	list<User>::iterator it;


	// find the user ..
	for(it=users_.begin();it!=users_.end();++it) {
		if((*it).getName()==u.getName()) break;
	}

	// succeed, return true ..
	if(it!=users_.end()) {
		(*it).setPhone(newPhone);
		return true;
	}

	// fail, return false ..
	return false;
}

list<User> UserManage::listAllUsers( ) {
	// return the list of all users_ ..

	// return users_ ..
	return users_;
}

void UserManage::initUsersList( std::list<User> users )
{
	this->users_ = users;
}

std::list<User> UserManage::acquireUsersList( void )
{
	return this->users_;
}


#endif

